package io.renren.modules.sys.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.utils.Tools;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.DepotEntity;
import io.renren.modules.sys.service.DepotService;
import io.renren.modules.sys.service.UserBusinessService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 仓库表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-11-22 14:29:35
 */
@RestController
@RequestMapping("sys/depot")
public class DepotController {
    @Autowired
    private DepotService depotService;
    @Autowired
    private UserBusinessService userBusinessService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:depot:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = depotService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/findBy")
    public R findBy(@RequestParam Map<String, Object> params){
//        limit page
        String pageSize = "15";
        String pageNo = "1";
        Object pageSizeObj = params.get("pageSize");
        Object pageNoObj = params.get("pageNo");
        if(pageSizeObj!=null){
            pageSize=pageSizeObj.toString();
        }
        if(pageNoObj!=null){
            pageNo=pageNoObj.toString();
        }
        params.put("limit",pageSize);
        params.put("page",pageNo);
        Page<DepotEntity> page = depotService.selectPage(
                new Query<DepotEntity>(params).getPage(),
                new EntityWrapper<DepotEntity>()
                        .orderBy("Id")
        );

        PageUtils pageUtils = new PageUtils(page);

        return R.ok().put("total",pageUtils.getTotalCount()).put("rows",pageUtils.getList());
    }

    @RequestMapping("/findGiftByType")
    public JSONArray findGiftByType(@RequestParam Map<String, Object> params){
//        limit page
        List<DepotEntity> list = depotService.selectList(new EntityWrapper<DepotEntity>().like("type", Tools.dealNullStr(params.get("type"))).orderBy("Id"));
        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != list) {
            for (DepotEntity depot : list) {
                JSONObject item = new JSONObject();
                item.put("id", depot.getId());
                //仓库名称
                item.put("name", depot.getName());
                dataArray.add(item);
            }
        }
        return dataArray;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:depot:info")
    public R info(@PathVariable("id") Long id){
        DepotEntity depot = depotService.selectById(id);

        return R.ok().put("depot", depot);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:depot:save")
    public R save(@RequestBody DepotEntity depot){
        depotService.insert(depot);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:depot:update")
    public R update(@RequestBody DepotEntity depot){
        ValidatorUtils.validateEntity(depot);
        depotService.updateAllColumnById(depot);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:depot:delete")
    public R delete(@RequestBody Long[] ids){
        depotService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    @RequestMapping("/getBasicData")
    public R getBasicData(@RequestParam Map<String, Object> params) {
        List<DepotEntity> list = depotService.selectByMap(params);

        return R.ok().put("total",list.size()).put("rows",list);
    }


    /**
     * 根据用户查找对应仓库列表-仅显示有权限的
     *
     * @return
     */
    @RequestMapping("/findDepotByUserId")
    public JSONArray findDepotByUserId(@RequestParam Map<String, Object> params) {

        List<DepotEntity> dataList = depotService.findDepotByMap(params);

        //存放数据json数组
        JSONArray dataArray = new JSONArray();
        if (null != dataList) {
            for (DepotEntity depot : dataList) {
                JSONObject item = new JSONObject();
                //勾选判断1
                Boolean flag = false;
                params.put("Value",depot.getId());
                try {
                    flag = userBusinessService.checkIsUserBusinessExist(params);
                } catch (Exception e) {
                }
                if (flag == true) {
                    item.put("id", depot.getId());
                    item.put("depotName", depot.getName());
                    dataArray.add(item);
                }
            }
        }
        //回写查询结果
        return dataArray;

    }

    /**
     * 修改
     */
    @RequestMapping("/updateDepot")
    @RequiresPermissions("sys:depot:update")
    public R updateDepot(@RequestParam Map<String, Object> params){
        DepotEntity depot = depotService.selectById(params.get("DepotID").toString());
        depot.setName((String)params.get("Name"));
        depot.setAddress((String)params.get("Address"));
        depot.setWarehousing(Double.valueOf((String)params.get("Warehousing")));
        depot.setTruckage(Double.valueOf((String)params.get("Truckage")));
        depot.setType((int)params.get("Type"));
        depot.setSort((String)params.get("Sort"));
        depot.setRemark((String)params.get("Remark"));
        ValidatorUtils.validateEntity(depot);
        depotService.updateAllColumnById(depot);//全部更新

        return R.ok();
    }

}
